package com.larly.controller;


import cn.hutool.core.util.ObjUtil;
import com.larly.common.BaseResponse;
import com.larly.common.IdRequest;
import com.larly.common.ResultUtils;
import com.larly.exception.ErrorCode;
import com.larly.exception.ThrowUtils;
import com.larly.model.domain.Interactions;
import com.larly.model.dto.Interactions.AddInteractions;
import com.larly.model.dto.Interactions.EditInteractionsRequest;
import com.larly.model.vo.InteractionsVO;
import com.larly.service.InteractionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/interactions")
@Slf4j
@Api(tags = "互动模块")
public class InteractionsController {

    @Resource
    private InteractionsService interactionsService;

    /**
     * 添加互动
     */
    @PostMapping("/add")
    @ApiOperation("添加互动")
    public BaseResponse<Integer> addInteractions(@RequestBody AddInteractions addInteractions, HttpServletRequest request) {
        Integer id = interactionsService.addInteractions(addInteractions, request);
        return ResultUtils.success(id);
    }

    /**
     * 获取笔记下的所有互动
     */
    @PostMapping("/list")
    @ApiOperation("获取笔记下的所有互动")
    public BaseResponse<List<InteractionsVO>> getInteractionsByNoteId(@RequestBody IdRequest idRequest) {
        List<InteractionsVO> interactionsList = interactionsService.getInteractionsByNoteId(idRequest);
        return ResultUtils.success(interactionsList);
    }

    /**
     * 点赞
     */
    @PostMapping("/like")
    @ApiOperation("点赞")
    public BaseResponse<Boolean> likeInteractions(@RequestBody IdRequest idRequest, HttpServletRequest request) {
        boolean result = interactionsService.likeInteractions(idRequest, request);
        return ResultUtils.success(result);
    }

    /**
     * 取消点赞
     */
    @PostMapping("/cancelLike")
    @ApiOperation("取消点赞")
    public BaseResponse<Boolean> cancelLikeInteractions(@RequestBody IdRequest idRequest, HttpServletRequest request) {
        boolean result = interactionsService.cancelLikeInteractions(idRequest, request);
        return ResultUtils.success(result);
    }

    /**
     * 根据id获取互动
     */
    @GetMapping("/get")
    @ApiOperation("根据id获取互动")
    public BaseResponse<InteractionsVO> getInteractionsById(@RequestParam Integer interactionsId) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(interactionsId), ErrorCode.PARAM_ERROR, "互动ID不能为空");
        Interactions interactions = interactionsService.getById(interactionsId);
        InteractionsVO interactionsVo = InteractionsVO.toVO(interactions);
        return ResultUtils.success(interactionsVo);
    }

    /**
     * 修改互动
     */
    @PostMapping("/edit")
    @ApiOperation("编辑互动")
    public BaseResponse<Boolean> editInteractions(@RequestBody EditInteractionsRequest editInteractionsRequest, HttpServletRequest request) {
        boolean result = interactionsService.editInteractions(editInteractionsRequest, request);
        return ResultUtils.success(result);
    }
}
